<template>
  <el-button size="small" type="warning" :plain="!curState" @click="setGood" :loading="loading">
    {{ curState ? '取消精华帖' : '设为精华帖' }}
  </el-button>
</template>

<script>
import { formatIssue } from '@/views/issues/format';

export default {
  name: 'set-good',
  props: {
    issue: Object,
  },
  data() {
    return {
      loading: false,
    };
  },
  computed: {
    curState() { return this.issue.good; },
  },
  methods: {
    setGood() {
      this.loading = true;
      this.$api.spaceIssues.essence({
        spaceId: this.issue.space.id,
        issueId: this.issue.id,
        state: !this.curState,
      }).then((res) => {
        this.$emit('updated', formatIssue(res.data));
      }).catch(() => {})
        .then(() => { this.loading = false; });
    },
  },
};
</script>

<style></style>
